﻿Public Class Report
    Dim dt As New DataTable

    Private Sub Report_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'With DataGridView1
        '    .EnableHeadersVisualStyles = False
        'End With
        'With DataGridView2
        '    .EnableHeadersVisualStyles = False
        'End With
        'With DataGridView3
        '    .EnableHeadersVisualStyles = False
        'End With

        'Dim columnHeaderStyle As New DataGridViewCellStyle
        ''columnHeaderStyle.BackColor = Color.LightSkyBlue
        ''columnHeaderStyle.ForeColor = Color.Black
        'columnHeaderStyle.Font = _
        '    New Font("Khmer OS Bokor", 10, FontStyle.Bold)
        'DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
        'DataGridView2.ColumnHeadersDefaultCellStyle = columnHeaderStyle
        'DataGridView3.ColumnHeadersDefaultCellStyle = columnHeaderStyle
        UI.FormatDataGrid(DataGridView1)
        UI.FormatDataGrid(DataGridView2)
        UI.FormatDataGrid(DataGridView3)

        loadPos()
        loadRole()
        loadUnit()

        loadfemale()
        loadfemalepos()
        loadfemalerole()
        'loadkid()
       
    End Sub

    Private Sub loadPos()
        DataGridView1.DataSource = cmd.ExcecuteDatatable("SELECT e.PositionId, p.PositionName,Count(e.PositionId) as [Total] FROM Employee e INNER JOIN [Position] p ON e.PositionId=p.PositionId Group By e.PositionId,p.PositionName")
    End Sub
    Private Sub loadRole()
        DataGridView2.DataSource = cmd.ExcecuteDatatable("SELECT e.RoleId, r.RoleName,Count(e.RoleId) as [Total] FROM Employee e INNER JOIN [Role] r ON e.RoleId=r.RoleId Group By e.RoleId,r.RoleName")
    End Sub
    Private Sub loadUnit()
        dt = cmd.ExcecuteDatatable("SELECT e.MinistryId, m.MinistryName,Count(e.MinistryId) as [Total] FROM Employee e INNER JOIN [Ministry] m ON e.MinistryId=m.MinistryId Group By e.MinistryId,m.MinistryName")
        DataGridView3.DataSource = dt
        'loadfemale()
    End Sub
    Private Sub loadfemale()
        For i = 0 To DataGridView3.Rows.Count - 1
            If DataGridView3.Rows(i).Cells("minid").Value > 0 Then
                'MessageBox.Show(DataGridView3.Rows(i).Cells(1).Value)
                cmdp.Parameters("@min") = DataGridView3.Rows(i).Cells("minid").Value
                DataGridView3.Rows(i).Cells("female").Value = cmdp.ExecuteScalar("SELECT COUNT(*) FROM Employee WHERE MinistryId=@min AND SexId=2")
            End If
        Next
    End Sub

    Private Sub loadfemalepos()
        For i = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells("poid").Value > 0 Then
                'MessageBox.Show(DataGridView3.Rows(i).Cells(1).Value)
                cmdp.Parameters("@pos") = DataGridView1.Rows(i).Cells("poId").Value
                DataGridView1.Rows(i).Cells(0).Value = cmdp.ExecuteScalar("SELECT COUNT(*) FROM Employee WHERE PositionId=@pos AND SexId=2")
            End If
        Next
    End Sub
    Private Sub loadfemalerole()
        For i = 0 To DataGridView2.Rows.Count - 1
            If DataGridView2.Rows(i).Cells("roleid").Value > 0 Then
                'MessageBox.Show(DataGridView3.Rows(i).Cells(1).Value)
                cmdp.Parameters("@role") = DataGridView2.Rows(i).Cells("RoleId").Value
                DataGridView2.Rows(i).Cells(0).Value = cmdp.ExecuteScalar("SELECT COUNT(*) FROM Employee WHERE RoleId=@role AND SexId=2")
            End If
        Next
    End Sub

    Private Sub loadkid()
        For i = 0 To DataGridView3.Rows.Count - 1
            If DataGridView3.Rows(i).Cells("minid").Value > 0 Then
                cmdp.Parameters("@min") = DataGridView3.Rows(i).Cells("minid").Value
                DataGridView3.Rows(i).Cells("kid").Value = cmdp.ExecuteScalar("SELECT COUNT(*) FROM [Relative] r INNER JOIN [Employee] e ON e.EmpId=r.EmpId WHERE e.MinistryId=@min AND r.TypeId=2 AND r.Active=true")
            End If
        Next
    End Sub
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub DataGridView3_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView3.DoubleClick
        If Me.DataGridView3.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView3.SelectedRows(0)
        Dim frm As New UnitList
        frm.minid = row.Cells("minid").Value
        frm.ShowDialog()
    End Sub

    Private Sub DataGridView3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView3.Click
        loadfemale()
        loadkid()
    End Sub

    Private Sub DataGridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
        loadfemalepos()
    End Sub

    Private Sub DataGridView2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView2.Click
        loadfemalerole()
    End Sub

    Private Sub btndes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndes.Click
        DataGridView3_DoubleClick(sender, e)
    End Sub

    
    Private Sub មលមនរតតមអងគភពToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles មលមនរតតមអងគភពToolStripMenuItem.Click
        DataGridView3_DoubleClick(sender, e)
    End Sub
End Class